/**
 * Created by sky_wang on 2017/4/14.
 */

REPORT_VIEW.REPORT_CONSULTED = {};
var REPORT_READ = REPORT_VIEW.REPORT_CONSULTED;
REPORT_READ.data = new Array();
REPORT_READ.view = new Object();
REPORT_READ.queryAllObj = new Array();
REPORT_READ.options= {
    columnDefs             : [],
    rowData                : [],
    enableSorting: true,
    enableColResize:true,
    rowHeight: 40,
    colWidth: 100,
    showToolPanel:false,
    suppressColumnVirtualisation:true,
    toolPanelSuppressRowGroups:true,
    toolPanelSuppressValues: true,
    toolPanelSuppressPivots: true,
    toolPanelSuppressPivotMode: true,
};
REPORT_READ.refreshData = function (){
    $(".all_show_select_report_options").empty();
    $(".all_show_select_report_options").append("<option value='default'>---请选择---</option>");
    $.get("report/list", function(data) {
        REPORT_READ.data = data;
        console.log(data);
        for (var i =0; i < data.length; i++)
        {
            if (null == data[i]["report_name"])
            {
                continue;
            }
            $(".all_show_select_report_options").append("<option value='"+data[i]["vid"]+"'>"+data[i]["report_name"]+"</option>");
        }
    });
    $.get("view/list", function(data) {
        REPORT_READ.view.data = data;
    });
}
$(document).ready(function() {
    REPORT_READ.refreshData();
    $('.all_show_content').hide();

    var gridDiv = document.querySelector('.all_show_grid');
    new agGrid.Grid(gridDiv, REPORT_READ.options);
    $(".all_show_select_report_options").change(function(){
        $('.all_show_content').hide();
    });
    $(".all_show_confirm_report").click(function () {
        if ($(".all_show_select_report_options").val() == 'default')
        {
            alert("请选择报表")
            return;
        }

        var report_gird_id = '';
        REPORT_READ.data.map(function (ele){
            if ($(".all_show_select_report_options").val() == ele["vid"]) {
                report_gird_id = ele["view_for_grid_id"];
            }
        });
        if (report_gird_id =='')
        {
            alert("报表模版错误")
            return;
        }
        var grid = new Object();
        REPORT_READ.view.data.map(function (ele){
            if (report_gird_id == ele["vid"]) {
                grid = ele;
                console.log("grid:" + grid);

                REPORT_READ.options.api.setColumnDefs(JSON.parse(grid["cols_of_sub_groups"]));

                //var allColumnIds = [];
                //(JSON.parse(grid["cols_of_sub_groups"])).forEach( function(columnDef) {
                //    allColumnIds.push(columnDef.field);
                //});
                //REPORT_READ.options.columnApi.setColumnsVisible(allColumnIds,true);
                REPORT_READ.options.api.refreshView();
                //REPORT_READ.options.api.sizeColumnsToFit();

            }
        });
        $('.all_show_content').show();
        $.ajax({
            type : "get",
            url  : "report/"+ $(".all_show_select_report_options").val(),
            data : {},
            async : true,
            success : function(data){
                REPORT_READ.options.api.setRowData(JSON.parse(data["report_data"]));
                //REPORT_READ.options.columnApi.autoSizeColumns(allColumnIds);
            }
        });

    });



});